<template>
  <el-dialog :title="dialogTit" :visible.sync="formVisible" width="520px" label-width="auto">
    <el-form :model="form" :rules="rules" class="tanBlank" ref="submitForm" v-loading="showDialogNews">
      <el-row :gutter="20">
        <el-col :span="6" style="text-align: right">
          <span style="color:#f30">*</span> 标题：
        </el-col>
        <el-col :span="16">
           <el-form-item prop="title">
            <el-input v-model="form.title" clearable placeholder="请输入标题" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row :gutter="20">
        <el-col :span="6" style="text-align: right">匹配股票代码：</el-col>
        <el-col :span="16">
           <el-form-item prop="stockCode">
            <el-input v-model="form.stockCode" clearable placeholder="请输入股票代码" />
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row :gutter="20">
        <el-col :span="6" style="text-align: right">更新频率(H)：</el-col>
        <el-col :span="16">
           <el-form-item prop="renewalCycle">
            <el-input v-model="form.renewalCycle" clearable placeholder="请输入更新频率" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6" style="text-align: right">资讯内容：</el-col>
        <el-col :span="16">
          <el-form-item prop="content">
            <el-input class="input-box-auto" v-model="form.content"  type="textarea" placeholder="请输入内容"  rows="6" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" class="black"  type="primary" @click="sumbit">提交</el-button>
      <el-button size="mini" @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { findCompanyAdd, findCompanyEditPage, findCompanyEditSave } from '@/child-business/api/news'
  export default {
    name: 'DialogNews',
    props: {
      dialogTit: { type: String, default: '添加' },
      formVisible: { type: Boolean, required: true, default: false },
      newsId: { type: String, default: '' },
      newsType: {type: Number, default: 1}
    },
    data() {
      return {
        form: {
          renewalCycle: '',
          title: '',
          content: '',
          stockCode: ''
        },
        rules: {
          title: [
            { required: true, message: '请输入标题内容', trigger: 'blur' },
          ],
        },
        showDialogNews: false
      }
    },
    // 监听数据变化
    watch: {
      newsId: {
        handler(val){
          if(val){
            this.getNewsInfo(val) // 如果有值，则获取对应的新闻数据
          } else {
            this.form = {
              renewalCycle: '',
              title: '',
              content: '',
              stockCode: ''
            }
          }
        }
      }
    },
    mounted() {
      this.form.newsType = this.newsType // 当前表单提交类型  1是资讯，2是政策
    },
    methods: {
      handleClose() {
        this.$emit('update:formVisible', false)
      },
      // 按钮判断，是添加还是修改，
      sumbit(){
        this.showDialogNews = true
        if(!this.newsId) { // 如果没有传值，那么这里就是添加
          this.$refs['submitForm'].validate((valid) => {
            if (valid) {
              findCompanyAdd(this.form).then(res => {
                if(res) {
                  this.$message({ type: 'success', message: '添加信息成功' })
                  this.$emit('update:formVisible', false)
                }else{
                  this.$message({ type: 'warning', message: '添加信息失败' })
                }
                this.$emit('getList')
                this.showDialogNews = false
              })
            }
          })
        } else { // 提交修改
          findCompanyEditSave({ ...this.form, id: this.newsId }).then(response => {
            this.$emit('update:formVisible', false)
            this.$message({ type: 'success', message: '提交成功' })
            this.$emit('getList')
            this.showDialogNews = false
          })
        }
      },
      // 获取对应的资讯数据
      getNewsInfo(id){
        this.showDialogNews = true
        findCompanyEditPage({ id: id }).then(response => {
          console.log(response)
          this.form = response.companyNewsVo
          this.showDialogNews = false
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
@import '@/child-business/styles/news.scss';
::v-deep .tanBlank .el-form-item{
  display: block;
}
::v-deep .el-dialog__body{padding: 20px;}
</style>
